home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / tcl.el < prev    next >
Encoding:
Text File  |  1995-07-28  |  72.6 KB  |  2,173 lines

  1. ;; tcl.el --- Tcl code editing commands for Emacs
  2.  
  3. ;; Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: Tom Tromey <tromey@busco.lanl.gov>
  6. ;; Author: Tom Tromey <tromey@busco.lanl.gov>
  7. ;;    Chris Lindblad <cjl@lcs.mit.edu>
  8. ;; Keywords: languages tcl modes
  9. ;; Version: 1.43
  10.  
  11. ;; This file is part of XEmacs.
  12.  
  13. ;; XEmacs is free software; you can redistribute it and/or modify it
  14. ;; under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; XEmacs is distributed in the hope that it will be useful, but
  19. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21. ;; General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  25. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27. ;; HOW TO INSTALL:
  28. ;; Put the following forms in your .emacs to enable autoloading of Tcl
  29. ;; mode, and auto-recognition of ".tcl" files.
  30. ;;
  31. ;;   (autoload 'tcl-mode "tcl" "Tcl mode." t)
  32. ;;   (autoload 'inferior-tcl "tcl" "Run inferior Tcl process." t)
  33. ;;   (setq auto-mode-alist (append '(("\\.tcl$" . tcl-mode)) auto-mode-alist))
  34. ;;
  35. ;; If you plan to use the interface to the TclX help files, you must
  36. ;; set the variable tcl-help-directory-list to point to the topmost
  37. ;; directories containing the TclX help files.  Eg:
  38. ;;
  39. ;;   (setq tcl-help-directory-list '("/usr/local/lib/tclx/help"))
  40. ;;
  41. ;; Also you will want to add the following to your .emacs:
  42. ;;
  43. ;;   (autoload 'tcl-help-on-word "tcl" "Help on Tcl commands" t)
  44. ;;
  45. ;; FYI a *very* useful thing to do is nroff all the Tk man pages and
  46. ;; put them in a subdir of the help system.
  47. ;;
  48.  
  49. ;;; Commentary:
  50.  
  51. ;; LCD Archive Entry:
  52. ;; tcl|Tom Tromey|tromey@busco.lanl.gov|
  53. ;; Major mode for editing Tcl|
  54. ;; 1995/07/17 19:59:49|1.43|~/modes/tcl.el.Z|
  55.  
  56. ;; CUSTOMIZATION NOTES:
  57. ;; * tcl-proc-list can be used to customize a list of things that
  58. ;; "define" other things.  Eg in my project I put "defvar" in this
  59. ;; list.
  60. ;; * tcl-typeword-list is similar, but uses font-lock-type-face.
  61. ;; * tcl-keyword-list is a list of keywords.  I've generally used this
  62. ;; for flow-control words.  Eg I add "unwind_protect" to this list.
  63. ;; * tcl-type-alist can be used to minimally customize indentation
  64. ;; according to context.
  65.  
  66. ;; Change log:
  67. ;; tcl.el,v
  68. ;; Revision 1.43  1995/07/17  19:59:49  tromey
  69. ;; (inferior-tcl-mode): Use modeline-process if it exists.
  70. ;;
  71. ;; Revision 1.42  1995/07/17  19:55:25  tromey
  72. ;; XEmacs currently must use tcl-internal-end-of-defun
  73. ;;
  74. ;; Revision 1.41  1995/07/14  21:54:56  tromey
  75. ;; Changes to make menus work in XEmacs.
  76. ;; From Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
  77. ;;
  78. ;; Revision 1.40  1995/07/11  03:13:15  tromey
  79. ;; (tcl-mode): Customize for new dabbrev.
  80. ;;
  81. ;; Revision 1.39  1995/07/09  21:58:03  tromey
  82. ;; (tcl-do-fill-paragraph): New function.
  83. ;; (tcl-mode): Set up for paragraph filling.
  84. ;;
  85. ;; Revision 1.38  1995/07/09  21:30:32  tromey
  86. ;; (tcl-mode): Fixes to 19.29 paragraph variables.
  87. ;;
  88. ;; Revision 1.37  1995/07/09  18:52:16  tromey
  89. ;; (tcl-do-auto-fill): Set fill-prefix.
  90. ;;
  91. ;; Revision 1.36  1995/07/09  01:07:57  tromey
  92. ;; (tcl-imenu-create-index-function): Work with imenu from Emacs 19.29
  93. ;;
  94. ;; Revision 1.35  1995/06/27  20:12:00  tromey
  95. ;; (tcl-type-alist): More itcl changes.
  96. ;;
  97. ;; Revision 1.34  1995/06/27  20:06:05  tromey
  98. ;; More changes for itcl.
  99. ;; Bug fixes for Emacs 19.29.
  100. ;;
  101. ;; Revision 1.33  1995/06/27  20:01:29  tromey
  102. ;; (tcl-set-proc-regexp): Allow leading spaces.
  103. ;; (tcl-proc-list): Changes for itcl.
  104. ;; (tcl-typeword-list): Ditto.
  105. ;; (tcl-keyword-list): Ditto.
  106. ;;
  107. ;; Revision 1.32  1995/05/11  22:12:49  tromey
  108. ;; (tcl-type-alist): Include entry for "proc".
  109. ;;
  110. ;; Revision 1.31  1995/05/10  23:38:12  tromey
  111. ;; (tcl-add-fsf-menu): Use make-lucid-menu-keymap, not
  112. ;; "make-xemacs-menu-keymap".
  113. ;;
  114. ;; Revision 1.30  1995/05/10  18:22:21  tromey
  115. ;; Bug fix in menu code for XEmacs.
  116. ;;
  117. ;; Revision 1.29  1995/05/09  21:36:53  tromey
  118. ;; Changed "Lucid Emacs" to "XEmacs".
  119. ;; Tcl's popup menu now added to existing one, courtesy
  120. ;; dfarmer@evolving.com (Doug Farmer)
  121. ;;
  122. ;; Revision 1.28  1995/04/08  19:52:50  tromey
  123. ;; (tcl-outline-level): New function
  124. ;; (tcl-mode): Added outline-handling stuff.
  125. ;; From Jesper Pedersen <blackie@imada.ou.dk>
  126. ;;
  127. ;; Revision 1.27  1994/10/11  02:01:27  tromey
  128. ;; (tcl-mode): imenu-create-index-function made buffer local.
  129. ;;
  130. ;; Revision 1.26  1994/09/01  18:06:24  tromey
  131. ;; Added filename completion in inferior tcl mode
  132. ;;
  133. ;; Revision 1.25  1994/08/22  15:56:24  tromey
  134. ;; tcl-load-file default to current buffer.
  135. ;;
  136. ;; Revision 1.24  1994/08/21  20:33:05  tromey
  137. ;; Fixed bug in tcl-guess-application.
  138. ;;
  139. ;; Revision 1.23  1994/08/21  03:54:45  tromey
  140. ;; Keybindings don't overshadown comint bindings.
  141. ;;
  142. ;; Revision 1.22  1994/07/26  00:46:07  tromey
  143. ;; Emacs 18 changes from Carl Witty.
  144. ;;
  145. ;; Revision 1.21  1994/07/14  22:49:21  tromey
  146. ;; Added ";;;###autoload" comments where appropriate.
  147. ;;
  148. ; Revision 1.20  1994/06/05  16:57:22  tromey
  149. ; tcl-current-word does the right thing in inferior-tcl-mode.
  150. ;
  151. ; Revision 1.19  1994/06/03  21:09:19  tromey
  152. ; Another menu fix.
  153. ;
  154. ; Revision 1.18  1994/06/03  20:39:14  tromey
  155. ; Fixed menu bug.
  156. ;
  157. ; Revision 1.17  1994/06/03  00:47:15  tromey
  158. ; Fixed bug in bug-reporting code.
  159. ;
  160. ; Revision 1.16  1994/05/26  05:06:14  tromey
  161. ; Menu items now sensitive as appropriate.
  162. ;
  163. ; Revision 1.15  1994/05/22  20:38:11  tromey
  164. ; Added bug-report keybindings and menu entries.
  165. ;
  166. ; Revision 1.14  1994/05/22  20:18:28  tromey
  167. ; Even more compile stuff.
  168. ;
  169. ; Revision 1.13  1994/05/22  20:17:15  tromey
  170. ; Moved emacs version checking code to very beginning.
  171. ;
  172. ; Revision 1.12  1994/05/22  20:14:59  tromey
  173. ; Compile fixes.
  174. ;
  175. ; Revision 1.11  1994/05/22  20:12:44  tromey
  176. ; Fixed mark-defun for 19.23.
  177. ; More menu fixes.
  178. ;
  179. ; Revision 1.10  1994/05/22  20:02:03  tromey
  180. ; Fixed bug with M-;.
  181. ; Wrote bug-reporting code.
  182. ;
  183. ; Revision 1.9  1994/05/22  05:26:51  tromey
  184. ; Fixes for imenu.
  185. ;
  186. ; Revision 1.8  1994/05/22  03:38:07  tromey
  187. ; Fixed menu support.
  188. ;
  189. ; Revision 1.7  1994/05/03  01:23:42  tromey
  190. ; *** empty log message ***
  191. ;
  192. ; Revision 1.6  1994/04/23  16:23:36  tromey
  193. ; Wrote tcl-indent-for-comment
  194. ;
  195. ;;
  196. ;; 18-Mar-1994        Tom Tromey    Fourth beta release.
  197. ;;    Added {un,}comment-region to menu.  Idea from
  198. ;;    Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
  199. ;; 17-Mar-1994        Tom Tromey    
  200. ;;    Fixed tcl-restart-with-file.  Bug fix attempt in
  201. ;;    tcl-internal-end-of-defun.
  202. ;; 16-Mar-1994        Tom Tromey    Third beta release
  203. ;;    Added support code for menu (from Tcl mode written by
  204. ;;    schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)).
  205. ;; 12-Mar-1994        Tom Tromey    
  206. ;;    Better documentation for inferior-tcl-buffer.  Wrote
  207. ;;    tcl-restart-with-file.  Wrote Lucid Emacs menu (but no
  208. ;;    code to install it).
  209. ;; 12-Mar-1994        Tom Tromey    
  210. ;;    Wrote tcl-guess-application.  Another stab at making
  211. ;;    tcl-omit-ws-regexp work.
  212. ;; 10-Mar-1994        Tom Tromey    Second beta release
  213. ;;    Last Modified: Thu Mar 10 01:24:25 1994 (Tom Tromey)
  214. ;;    Wrote perl-mode style line indentation command.
  215. ;;    Wrote more documentation.  Added tcl-continued-indent-level.
  216. ;;    Integrated help code.
  217. ;; 8-Mar-1994        Tom Tromey    
  218. ;;    Last Modified: Tue Mar  8 11:58:44 1994 (Tom Tromey)
  219. ;;    Bug fixes.
  220. ;; 6-Mar-1994        Tom Tromey    
  221. ;;    Last Modified: Sun Mar  6 18:55:41 1994 (Tom Tromey)
  222. ;;    Updated auto-newline support.
  223. ;; 6-Mar-1994        Tom Tromey    Beta release
  224. ;;    Last Modified: Sat Mar  5 17:24:32 1994 (Tom Tromey)
  225. ;;    Wrote tcl-hashify-buffer.  Other minor bug fixes.
  226. ;; 5-Mar-1994        Tom Tromey    
  227. ;;    Last Modified: Sat Mar  5 16:11:20 1994 (Tom Tromey)
  228. ;;    Wrote electric-hash code.
  229. ;; 3-Mar-1994        Tom Tromey    
  230. ;;    Last Modified: Thu Mar  3 02:53:40 1994 (Tom Tromey)
  231. ;;    Added code to handle auto-fill in comments.
  232. ;;    Added imenu support code.
  233. ;;    Cleaned up code.
  234. ;;    Better font-lock support.
  235. ;; 28-Feb-1994        Tom Tromey    
  236. ;;    Last Modified: Mon Feb 28 14:08:05 1994 (Tom Tromey)
  237. ;;    Made tcl-figure-type more easily configurable.
  238. ;; 28-Feb-1994        Tom Tromey    
  239. ;;    Last Modified: Mon Feb 28 01:02:58 1994 (Tom Tromey)
  240. ;;    Wrote inferior-tcl mode.
  241. ;; 16-Feb-1994        Tom Tromey    
  242. ;;    Last Modified: Wed Feb 16 17:05:19 1994 (Tom Tromey)
  243. ;;    Added support for font-lock-mode.
  244. ;; 29-Oct-1993        Tom Tromey    
  245. ;;    Last Modified: Sun Oct 24 17:39:14 1993 (Tom Tromey)
  246. ;;    Patches from Guido Bosch to make things work with Lucid Emacs.
  247. ;; 22-Oct-1993        Tom Tromey    
  248. ;;    Last Modified: Fri Oct 22 15:26:46 1993 (Tom Tromey)
  249. ;;    Made many characters have "_" syntax class; suggested by Guido
  250. ;;    Bosch <Guido.Bosch@loria.fr>.  Note that this includes the "$"
  251. ;;    character, which might be a change you'd notice.
  252. ;; 21-Oct-1993        Tom Tromey    
  253. ;;    Last Modified: Thu Oct 21 20:28:40 1993 (Tom Tromey)
  254. ;;    More fixes for tcl-omit-ws-regexp.
  255. ;; 20-Oct-1993        Tom Tromey    
  256. ;;    Started keeping history.  Fixed tcl-{beginning,end}-of-defun.
  257. ;;    Added some code to make things work with Emacs 18.
  258.  
  259. ;; THANKS TO:
  260. ;; Guido Bosch <Guido.Bosch@loria.fr>
  261. ;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma)
  262. ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
  263. ;; Matt Newman <men@charney.colorado.edu>
  264. ;; rwhitby@research.canon.oz.au (Rod Whitby)
  265. ;; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta])
  266. ;; Pertti Tapio Kasanen <ptk@delta.hut.fi>
  267. ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)
  268. ;; warsaw@nlm.nih.gov (Barry A. Warsaw)
  269. ;; Carl Witty <cwitty@ai.mit.edu>
  270. ;; T. V. Raman <raman@crl.dec.com>
  271. ;; Jesper Pedersen <blackie@imada.ou.dk>
  272. ;; dfarmer@evolving.com (Doug Farmer)
  273.  
  274. ;; KNOWN BUGS:
  275. ;; * indent-region should skip blank lines.  (It does in v19, so I'm
  276. ;;   not motivated to fix it here).
  277. ;; * In Tcl "#" is not always a comment character.  This can confuse
  278. ;;   tcl.el in certain circumstances.  For now the only workaround is
  279. ;;   to enclose offending hash characters in quotes or precede it with
  280. ;;   a backslash.  Note that using braces won't work -- quotes change
  281. ;;   the syntax class of characters between them, while braces do not.
  282. ;;   The electric-# mode helps alleviate this problem somewhat.
  283. ;; * indent-tcl-exp is untested.
  284. ;; * Doesn't work under Emacs 18 yet.
  285. ;; * There's been a report that font-lock does strange things under
  286. ;;   Lucid Emacs 19.6.  For instance in "proc foobar", the space
  287. ;;   before "foobar" is highlighted.
  288.  
  289. ;; TODO:
  290. ;; * make add-log-tcl-defun smarter.  should notice if we are in the
  291. ;;   middle of a defun, or between defuns.  should notice if point is
  292. ;;   on first line of defun (or maybe even in comments before defun).
  293. ;; * Allow continuation lines to be indented under the first argument
  294. ;;   of the preceeding line, like this:
  295. ;;      [list something \
  296. ;;            something-else]
  297. ;; * There is a request that indentation work like this:
  298. ;;        button .fred -label Fred \
  299. ;;                     -command {puts fred}
  300. ;; * Should have tcl-complete-symbol that queries the inferior process.
  301. ;; * Should have describe-symbol that works by sending the magic
  302. ;;   command to a tclX process.
  303. ;; * Need C-x C-e binding (tcl-eval-last-exp).
  304. ;; * Write indent-region function that is faster than indenting each
  305. ;;   line individually.
  306. ;; * tcl-figure-type should stop at "beginning of line" (only ws
  307. ;;   before point, and no "\" on previous line).  (see tcl-real-command-p).
  308. ;; * overrides some comint keybindings; fix.
  309. ;; * Trailing \ will eat blank lines.  Should deal with this.
  310. ;;   (this would help catch some potential bugs).
  311. ;; * Inferior should display in half the screen, not the whole screen.
  312. ;; * Indentation should deal with "switch".
  313. ;; * Consider writing code to find help files automatically (for
  314. ;;   common cases).
  315. ;; * `#' shouldn't insert `\#' when point is in string.
  316.  
  317.  
  318.  
  319. ;;; Code:
  320.  
  321. ;; I sure wish Emacs had a package that made it easy to extract this
  322. ;; sort of information.
  323. (defconst tcl-using-emacs-19 (string-match "19\\." emacs-version)
  324.   "Nil unless using Emacs 19 (XEmacs or FSF).")
  325.  
  326. ;; FIXME this will break on Emacs 19.100.
  327. (defconst tcl-using-emacs-19-23
  328.   (string-match "19\\.\\(2[3-9]\\|[3-9][0-9]\\)" emacs-version)
  329.   "Nil unless using Emacs 19-23 or later.")
  330.  
  331. (defconst tcl-using-xemacs-19 (string-match "XEmacs" emacs-version)
  332.   "Nil unless using XEmacs).")
  333.  
  334. (require 'comint)
  335.  
  336. ;; When compiling under GNU Emacs, load imenu during compilation.  If
  337. ;; you have 19.22 or earlier, comment this out, or get imenu.
  338. (and (fboundp 'eval-when-compile)
  339.      (eval-when-compile
  340.        (if (and (string-match "19\\." emacs-version)
  341.         (not (string-match "XEmacs" emacs-version)))
  342.        (require 'imenu))
  343.        ()))
  344.  
  345. (defconst tcl-version "1.43")
  346. (defconst tcl-maintainer "Tom Tromey <tromey@drip.colorado.edu>")
  347.  
  348. ;;
  349. ;; User variables.
  350. ;;
  351.  
  352. (defvar tcl-indent-level 4
  353.   "*Indentation of Tcl statements with respect to containing block.")
  354.  
  355. (defvar tcl-continued-indent-level 4
  356.   "*Indentation of continuation line relative to first line of command.")
  357.  
  358. (defvar tcl-auto-newline nil
  359.   "*Non-nil means automatically newline before and after braces
  360. inserted in Tcl code.")
  361.  
  362. (defvar tcl-tab-always-indent t
  363.   "*Control effect of TAB key.
  364. If t (the default), always indent current line.
  365. If nil and point is not in the indentation area at the beginning of
  366. the line, a TAB is inserted.
  367. Other values cause the first possible action from the following list
  368. to take place:
  369.  
  370.   1. Move from beginning of line to correct indentation.
  371.   2. Delete an empty comment.
  372.   3. Move forward to start of comment, indenting if necessary.
  373.   4. Move forward to end of line, indenting if necessary.
  374.   5. Create an empty comment.
  375.   6. Move backward to start of comment, indenting if necessary.")
  376.  
  377. (defvar tcl-use-hairy-comment-detector t
  378.   "*If not `nil', the the more complicated, but slower, comment
  379. detecting function is used.  This variable is only used in GNU Emacs
  380. 19 (the fast function is always used elsewhere).")
  381.  
  382. (defvar tcl-electric-hash-style 'smart
  383.   "*Style of electric hash insertion to use.
  384. Possible values are 'backslash, meaning that `\\' quoting should be
  385. done; `quote, meaning that `\"' quoting should be done; 'smart,
  386. meaning that the choice between 'backslash and 'quote should be
  387. made depending on the number of hashes inserted; or nil, meaning that
  388. no quoting should be done.  Any other value for this variable is
  389. taken to mean 'smart.  The default is 'smart.")
  390.  
  391. (defvar tcl-help-directory-list nil
  392.   "*List of topmost directories containing TclX help files")
  393.  
  394. (defvar tcl-use-smart-word-finder t
  395.   "*If not nil, use a better way of finding the current word when
  396. looking up help on a Tcl command.")
  397.  
  398. (defvar tcl-application "wish"
  399.   "*Name of Tcl application to run in inferior Tcl mode.")
  400.  
  401. (defvar tcl-command-switches nil
  402.   "*Switches to supply to `tcl-application'.")
  403.  
  404. (defvar tcl-prompt-regexp "^\\(% \\|\\)"
  405.   "*If not nil, a regexp that will match the prompt in the inferior process.
  406. If nil, the prompt is the name of the application with \">\" appended.
  407.  
  408. The default is \"^\\(% \\|\\)\", which will match the default primary
  409. and secondary prompts for tclsh and wish.")
  410.  
  411. (defvar inferior-tcl-source-command "source %s\n"
  412.   "*Format-string for building a Tcl command to load a file.
  413. This format string should use `%s' to substitute a file name
  414. and should result in a Tcl expression that will command the
  415. inferior Tcl to load that file.  The filename will be appropriately
  416. quoted for Tcl.")
  417.  
  418. ;;
  419. ;; Keymaps, abbrevs, syntax tables.
  420. ;;
  421.  
  422. (defvar tcl-mode-abbrev-table nil
  423.   "Abbrev table in use in Tcl-mode buffers.")
  424. (if tcl-mode-abbrev-table
  425.     ()
  426.   (define-abbrev-table 'tcl-mode-abbrev-table ()))
  427.  
  428. (defvar tcl-mode-map ()
  429.   "Keymap used in Tcl mode.")
  430.  
  431. (defvar tcl-mode-syntax-table nil
  432.   "Syntax table in use in Tcl-mode buffers.")
  433. (if tcl-mode-syntax-table
  434.     ()
  435.   (setq tcl-mode-syntax-table (make-syntax-table))
  436.   (modify-syntax-entry ?%  "_" tcl-mode-syntax-table)
  437.   (modify-syntax-entry ?@  "_" tcl-mode-syntax-table)
  438.   (modify-syntax-entry ?&  "_" tcl-mode-syntax-table)
  439.   (modify-syntax-entry ?*  "_" tcl-mode-syntax-table)
  440.   (modify-syntax-entry ?+  "_" tcl-mode-syntax-table)
  441.   (modify-syntax-entry ?-  "_" tcl-mode-syntax-table)
  442.   (modify-syntax-entry ?.  "_" tcl-mode-syntax-table)
  443.   (modify-syntax-entry ?:  "_" tcl-mode-syntax-table)
  444.   (modify-syntax-entry ?!  "_" tcl-mode-syntax-table)
  445.   (modify-syntax-entry ?$  "_" tcl-mode-syntax-table) ; FIXME use "'"?
  446.   (modify-syntax-entry ?/  "_" tcl-mode-syntax-table)
  447.   (modify-syntax-entry ?~  "_" tcl-mode-syntax-table)
  448.   (modify-syntax-entry ?<  "_" tcl-mode-syntax-table)
  449.   (modify-syntax-entry ?=  "_" tcl-mode-syntax-table)
  450.   (modify-syntax-entry ?>  "_" tcl-mode-syntax-table)
  451.   (modify-syntax-entry ?|  "_" tcl-mode-syntax-table)
  452.   (modify-syntax-entry ?\(  "()" tcl-mode-syntax-table)
  453.   (modify-syntax-entry ?\)  ")(" tcl-mode-syntax-table)
  454.   (modify-syntax-entry ?\;  "." tcl-mode-syntax-table)
  455.   (modify-syntax-entry ?\n ">   " tcl-mode-syntax-table)
  456.   (modify-syntax-entry ?\f ">   " tcl-mode-syntax-table)
  457.   (modify-syntax-entry ?# "<   " tcl-mode-syntax-table))
  458.  
  459. (defvar inferior-tcl-mode-map nil
  460.   "Keymap used in Inferior Tcl mode.")
  461.  
  462. ;; XEmacs menu.
  463. (defvar tcl-xemacs-menu
  464.   '(["Beginning of function" tcl-beginning-of-defun t]
  465.     ["End of function" tcl-end-of-defun t]
  466.     ["Mark function" tcl-mark-defun t]
  467.     ["Indent region" indent-region (tcl-mark)]
  468.     ["Comment region" comment-region (tcl-mark)]
  469.     ["Uncomment region" tcl-uncomment-region (tcl-mark)]
  470.     "----"
  471.     ["Show Tcl process buffer" inferior-tcl t]
  472.     ["Send function to Tcl process" tcl-eval-defun
  473.      (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
  474.     ["Send region to Tcl process" tcl-eval-region
  475.      (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
  476.     ["Send file to Tcl process" tcl-load-file
  477.      (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
  478.     ["Restart Tcl process with file" tcl-restart-with-file t]
  479.     "----"
  480.     ["Tcl help" tcl-help-on-word tcl-help-directory-list]
  481.     ["Send bug report" tcl-submit-bug-report t])
  482.   "XEmacs menu for Tcl mode.")
  483.  
  484. ;; GNU Emacs does menus via keymaps.  Do it in a function in case we
  485. ;; later decide to add it to inferior Tcl mode as well.
  486. (defun tcl-add-fsf-menu (map)
  487.   (define-key map [menu-bar] (make-sparse-keymap))
  488.   ;; This fails in Emacs 19.22 and earlier.
  489.   (require 'lmenu)
  490.   (let ((menu (make-lucid-menu-keymap "Tcl" tcl-xemacs-menu)))
  491.     (define-key map [menu-bar tcl] (cons "Tcl" menu))
  492.     ;; The following is intended to compute the key sequence
  493.     ;; information for the menu.  It doesn't work.
  494.     (x-popup-menu nil menu)))
  495.  
  496. (defun tcl-fill-mode-map ()
  497.   (define-key tcl-mode-map "{" 'tcl-electric-char)
  498.   (define-key tcl-mode-map "}" 'tcl-electric-brace)
  499.   (define-key tcl-mode-map "[" 'tcl-electric-char)
  500.   (define-key tcl-mode-map "]" 'tcl-electric-char)
  501.   (define-key tcl-mode-map ";" 'tcl-electric-char)
  502.   (define-key tcl-mode-map "#" 'tcl-electric-hash)
  503.   ;; FIXME.
  504.   (define-key tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
  505.   ;; FIXME.
  506.   (define-key tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
  507.   ;; FIXME.
  508.   (define-key tcl-mode-map "\e\C-h" 'tcl-mark-defun)
  509.   (define-key tcl-mode-map "\e\C-q" 'indent-tcl-exp)
  510.   (define-key tcl-mode-map "\177" 'backward-delete-char-untabify)
  511.   (define-key tcl-mode-map "\t" 'tcl-indent-command)
  512.   (define-key tcl-mode-map "\M-;" 'tcl-indent-for-comment)
  513.   (define-key tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
  514.   (define-key tcl-mode-map "\C-c\C-b" 'tcl-submit-bug-report)
  515.   (and (fboundp 'comment-region)
  516.        (define-key tcl-mode-map "\C-c\C-c" 'comment-region))
  517.   (define-key tcl-mode-map "\C-c\C-i" 'tcl-help-on-word)
  518.   (define-key tcl-mode-map "\C-c\C-v" 'tcl-eval-defun)
  519.   (define-key tcl-mode-map "\C-c\C-f" 'tcl-load-file)
  520.   (define-key tcl-mode-map "\C-c\C-t" 'inferior-tcl)
  521.   (define-key tcl-mode-map "\C-c\C-x" 'tcl-eval-region)
  522.   (define-key tcl-mode-map "\C-c\C-s" 'switch-to-tcl)
  523.  
  524.   ;; Make menus.
  525.   (if (and tcl-using-emacs-19 (not tcl-using-xemacs-19))
  526.       (progn
  527.     (tcl-add-fsf-menu tcl-mode-map))))
  528.  
  529. (defun tcl-fill-inferior-map ()
  530.   (define-key inferior-tcl-mode-map "\t" 'comint-dynamic-complete)
  531.   (define-key inferior-tcl-mode-map "\M-?"
  532.     'comint-dynamic-list-filename-completions)
  533.   (define-key inferior-tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
  534.   (define-key inferior-tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
  535.   (define-key inferior-tcl-mode-map "\177" 'backward-delete-char-untabify)
  536.   (define-key inferior-tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
  537.   (define-key inferior-tcl-mode-map "\C-c\C-b" 'tcl-submit-bug-report)
  538.   (define-key inferior-tcl-mode-map "\C-c\C-i" 'tcl-help-on-word)
  539.   (define-key inferior-tcl-mode-map "\C-c\C-v" 'tcl-eval-defun)
  540.   (define-key inferior-tcl-mode-map "\C-c\C-f" 'tcl-load-file)
  541.   (define-key inferior-tcl-mode-map "\C-c\C-t" 'inferior-tcl)
  542.   (define-key inferior-tcl-mode-map "\C-c\C-x" 'tcl-eval-region)
  543.   (define-key inferior-tcl-mode-map "\C-c\C-s" 'switch-to-tcl))
  544.  
  545. (if tcl-mode-map
  546.     ()
  547.   (setq tcl-mode-map (make-sparse-keymap))
  548.   (tcl-fill-mode-map))
  549.  
  550. (if inferior-tcl-mode-map
  551.     ()
  552.   ;; FIXME Use keymap inheritance here?  FIXME we override comint
  553.   ;; keybindings here.  Maybe someone has a better set?
  554.   (setq inferior-tcl-mode-map (copy-keymap comint-mode-map))
  555.   (tcl-fill-inferior-map))
  556.  
  557.  
  558. (defvar inferior-tcl-buffer nil
  559.   "*The current inferior-tcl process buffer.
  560.  
  561. MULTIPLE PROCESS SUPPORT
  562. ===========================================================================
  563. To run multiple Tcl processes, you start the first up with
  564. \\[inferior-tcl].  It will be in a buffer named `*inferior-tcl*'.
  565. Rename this buffer with \\[rename-buffer].  You may now start up a new
  566. process with another \\[inferior-tcl].  It will be in a new buffer,
  567. named `*inferior-tcl*'.  You can switch between the different process
  568. buffers with \\[switch-to-buffer].
  569.  
  570. Commands that send text from source buffers to Tcl processes -- like
  571. `tcl-eval-defun' or `tcl-load-file' -- have to choose a process to
  572. send to, when you have more than one Tcl process around.  This is
  573. determined by the global variable `inferior-tcl-buffer'.  Suppose you
  574. have three inferior Lisps running:
  575.     Buffer              Process
  576.     foo                 inferior-tcl
  577.     bar                 inferior-tcl<2>
  578.     *inferior-tcl*      inferior-tcl<3>
  579. If you do a \\[tcl-eval-defun] command on some Lisp source code, what
  580. process do you send it to?
  581.  
  582. - If you're in a process buffer (foo, bar, or *inferior-tcl*), 
  583.   you send it to that process.
  584. - If you're in some other buffer (e.g., a source file), you
  585.   send it to the process attached to buffer `inferior-tcl-buffer'.
  586. This process selection is performed by function `inferior-tcl-proc'.
  587.  
  588. Whenever \\[inferior-tcl] fires up a new process, it resets
  589. `inferior-tcl-buffer' to be the new process's buffer.  If you only run
  590. one process, this does the right thing.  If you run multiple
  591. processes, you can change `inferior-tcl-buffer' to another process
  592. buffer with \\[set-variable].")
  593.  
  594. ;;
  595. ;; Hooks and other customization.
  596. ;;
  597.  
  598. (defvar tcl-mode-hook nil
  599.   "Hook run on entry to Tcl mode.
  600.  
  601. Several functions exist which are useful to run from your
  602. `tcl-mode-hook' (see each function's documentation for more
  603. information):
  604.  
  605.   tcl-guess-application
  606.     Guesses a default setting for `tcl-application' based on any
  607.     \"#!\" line at the top of the file.
  608.   tcl-hashify-buffer
  609.     Quotes all \"#\" characters that don't correspond to actual
  610.     Tcl comments.  (Useful when editing code not originally created
  611.     with this mode).
  612.   tcl-auto-fill-mode
  613.     Auto-filling of Tcl comments.
  614.  
  615. Emacs 19 users can add functions to the hook with `add-hook':
  616.  
  617.    (add-hook 'tcl-mode-hook 'tcl-guess-application)
  618.  
  619. Emacs 18 users must use `setq':
  620.  
  621.    (setq tcl-mode-hook (cons 'tcl-guess-application tcl-mode-hook))")
  622.  
  623.  
  624. (defvar inferior-tcl-mode-hook nil
  625.   "Hook for customizing Inferior Tcl mode.")
  626.  
  627. (defvar tcl-proc-list
  628.   '("proc" "method" "itcl_class")
  629.   "List of commands whose first argument defines something.
  630. This exists because some people (eg, me) use \"defvar\" et al.
  631. Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'
  632. after changing this list.")
  633.  
  634. (defvar tcl-proc-regexp nil
  635.   "Regexp to use when matching proc headers.")
  636.  
  637. (defvar tcl-typeword-list
  638.   '("global" "upvar" "inherit" "public" "protected" "common")
  639.   "List of Tcl keywords denoting \"type\".  Used only for highlighting.
  640. Call `tcl-set-font-lock-keywords' after changing this list.")
  641.  
  642. ;; Generally I've picked control operators to be keywords.
  643. (defvar tcl-keyword-list
  644.   '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while"
  645.     "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return"
  646.     "uplevel" "constructor" "destructor" "itcl_class" "loop" "for_array_keys"
  647.     "for_recursive_glob" "for_file")
  648.   "List of Tcl keywords.  Used only for highlighting.
  649. Default list includes some TclX keywords.
  650. Call `tcl-set-font-lock-keywords' after changing this list.")
  651.  
  652. (defvar tcl-font-lock-keywords nil
  653.   "Keywords to highlight for Tcl.  See variable `font-lock-keywords'.
  654. This variable is generally set from `tcl-proc-regexp',
  655. `tcl-typeword-list', and `tcl-keyword-list' by the function
  656. `tcl-set-font-lock-keywords'.")
  657.  
  658. ;; FIXME need some way to recognize variables because array refs look
  659. ;; like 2 sexps.
  660. (defvar tcl-type-alist
  661.   '(
  662.     ("proc" nil tcl-expr tcl-commands)
  663.     ("method" nil tcl-expr tcl-commands)
  664.     ("destructor" tcl-commands)
  665.     ("constructor" tcl-commands)
  666.     ("expr" tcl-expr)
  667.     ("catch" tcl-commands)
  668.     ("if" tcl-expr "then" tcl-commands)
  669.     ("elseif" tcl-expr "then" tcl-commands)
  670.     ("elseif" tcl-expr tcl-commands)
  671.     ("if" tcl-expr tcl-commands)
  672.     ("while" tcl-expr tcl-commands)
  673.     ("for" tcl-commands tcl-expr tcl-commands tcl-commands)
  674.     ("foreach" nil nil tcl-commands)
  675.     ("for_file" nil nil tcl-commands)
  676.     ("for_array_keys" nil nil tcl-commands)
  677.     ("for_recursive_glob" nil nil nil tcl-commands)
  678.     ;; Loop handling is not perfect, because the third argument can be
  679.     ;; either a command or an expr, and there is no real way to look
  680.     ;; forward.
  681.     ("loop" nil tcl-expr tcl-expr tcl-commands)
  682.     ("loop" nil tcl-expr tcl-commands)
  683.     )
  684.   "Alist that controls indentation.
  685. \(Actually, this really only controls what happens on continuation lines).
  686. Each entry looks like `(KEYWORD TYPE ...)'.
  687. Each type entry describes a sexp after the keyword, and can be one of:
  688. * nil, meaning that this sexp has no particular type.
  689. * tcl-expr, meaning that this sexp is an arithmetic expression.
  690. * tcl-commands, meaning that this sexp holds Tcl commands.
  691. * a string, which must exactly match the string at the corresponding
  692.   position for a match to be made.
  693.  
  694. For example, the entry for the \"loop\" command is:
  695.  
  696.    (\"loop\" nil tcl-expr tcl-commands)
  697.  
  698. This means that the \"loop\" command has three arguments.  The first
  699. argument is ignored (for indentation purposes).  The second argument
  700. is a Tcl expression, and the last argument is Tcl commands.")
  701.  
  702. (defvar tcl-explain-indentation nil
  703.   "If not `nil', debugging message will be printed during indentation.")
  704.  
  705.  
  706.  
  707. ;;
  708. ;; Work around differences between various versions of Emacs.
  709. ;;
  710.  
  711. ;; We use this because Lemacs 19.9 has what we need.
  712. (defconst tcl-pps-has-arg-6
  713.   (or tcl-using-emacs-19
  714.       (and tcl-using-xemacs-19
  715.        (condition-case nil
  716.            (progn
  717.          (parse-partial-sexp (point) (point) nil nil nil t)
  718.          t)
  719.          (error nil))))
  720.   "t if using an emacs which supports sixth (\"commentstop\") argument
  721. to parse-partial-sexp.")
  722.  
  723. ;; Its pretty bogus to have to do this, but there is no easier way to
  724. ;; say "match not syntax-1 and not syntax-2".  Too bad you can't put
  725. ;; \s in [...].  This sickness is used in Emacs 19 to match a defun
  726. ;; starter.  (It is used for this in v18 as well).
  727. ;;(defconst tcl-omit-ws-regexp
  728. ;;  (concat "^\\(\\s"
  729. ;;      (mapconcat 'char-to-string "w_.()\"\\$'/" "\\|\\s")
  730. ;;      "\\)\\S(*")
  731. ;;  "Regular expression that matches everything except space, comment
  732. ;;starter, and comment ender syntax codes.")
  733.  
  734. ;; FIXME?  Instead of using the hairy regexp above, we just use a
  735. ;; simple one.
  736. ;;(defconst tcl-omit-ws-regexp "^[^] \t\n#}]\\S(*"
  737. ;;  "Regular expression used in locating function definitions.")
  738.  
  739. ;; Here's another stab.  I think this one actually works.  Now the
  740. ;; problem seems to be that there is a bug in Emacs 19.22 where
  741. ;; end-of-defun doesn't really use the brace matching the one that
  742. ;; trails defun-prompt-regexp.
  743. (defconst tcl-omit-ws-regexp "^[^ \t\n#}][^\n}]+}*[ \t]+")
  744.  
  745. (defun tcl-internal-beginning-of-defun (&optional arg)
  746.   "Move backward to next beginning-of-defun.
  747. With argument, do this that many times.
  748. Returns t unless search stops due to end of buffer."
  749.   (interactive "p")
  750.   (if (or (null arg) (= arg 0))
  751.       (setq arg 1))
  752.   (let (success)
  753.     (while (progn
  754.          (setq arg (1- arg))
  755.          (and (>= arg 0)
  756.           (setq success
  757.             (re-search-backward tcl-omit-ws-regexp nil 'move 1))))
  758.       (while (and (looking-at "[]#}]")
  759.           (setq success
  760.             (re-search-backward tcl-omit-ws-regexp nil 'move 1)))))
  761.     (beginning-of-line)
  762.     (not (null success))))
  763.  
  764. (defun tcl-internal-end-of-defun (&optional arg)
  765.   "Move forward to next end of defun.
  766. An end of a defun is found by moving forward from the beginning of one."
  767.   (interactive "p")
  768.   (if (or (null arg) (= arg 0)) (setq arg 1))
  769.   (let ((start (point)))
  770.     ;; Was forward-char.  I think this works a little better.
  771.     (forward-line)
  772.     (tcl-beginning-of-defun)
  773.     (while (> arg 0)
  774.       (while (and (re-search-forward tcl-omit-ws-regexp nil 'move 1)
  775.           (progn (beginning-of-line) t)
  776.           (looking-at "[]#}]")
  777.           (progn (forward-line) t)))
  778.       (let ((next-line (save-excursion 
  779.              (forward-line)
  780.              (point))))
  781.     (while (< (point) next-line)
  782.       (forward-sexp)))
  783.       (forward-line)
  784.       (if (> (point) start) (setq arg (1- arg))))))
  785.  
  786. ;; In Emacs 19, we can use begining-of-defun as long as we set up a
  787. ;; certain regexp.  In Emacs 18, we need our own function.
  788. (fset 'tcl-beginning-of-defun
  789.       (if tcl-using-emacs-19
  790.       'beginning-of-defun
  791.     'tcl-internal-beginning-of-defun))
  792.  
  793. ;; Ditto end-of-defun.
  794. (fset 'tcl-end-of-defun
  795.       (if (and tcl-using-emacs-19 (not tcl-using-xemacs-19))
  796.       'end-of-defun
  797.     'tcl-internal-end-of-defun))
  798.  
  799. ;; Internal mark-defun that is used for losing Emacsen.
  800. (defun tcl-internal-mark-defun ()
  801.   "Put mark at end of Tcl function, point at beginning."
  802.   (interactive)
  803.   (push-mark (point))
  804.   (tcl-end-of-defun)
  805.   (if tcl-using-emacs-19
  806.       (push-mark (point) nil t)
  807.     (push-mark (point)))
  808.   (tcl-beginning-of-defun)
  809.   (backward-paragraph))
  810.  
  811. ;; In GNU Emacs 19-23 and later, mark-defun works as advertised.  I
  812. ;; don't know about XEmacs, so for now it and Emacs 18 just lose.
  813. (fset 'tcl-mark-defun
  814.       (if tcl-using-emacs-19-23
  815.       'mark-defun
  816.     'tcl-internal-mark-defun))
  817.  
  818. ;; In GNU Emacs 19, mark takes an additional "force" argument.  I
  819. ;; don't know about XEmacs, so I'm just assuming it is the same.
  820. ;; Emacs 18 doesn't have this argument.
  821. (defun tcl-mark ()
  822.   "Return mark, or nil if none."
  823.   (if tcl-using-emacs-19
  824.       (mark t)
  825.     (mark)))
  826.  
  827.  
  828.  
  829. ;;
  830. ;; Some helper functions.
  831. ;;
  832.  
  833. (defun tcl-set-proc-regexp ()
  834.   "Set `tcl-proc-regexp' from variable `tcl-proc-list'."
  835.   (setq tcl-proc-regexp (concat "^\\s-*\\("
  836.                 (mapconcat 'identity tcl-proc-list "\\|")
  837.                 "\\)[ \t]+")))
  838.  
  839. (defun tcl-set-font-lock-keywords ()
  840.   "Set `tcl-font-lock-keywords'.
  841. Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
  842.   (setq tcl-font-lock-keywords
  843.     (list
  844.      ;; Names of functions (and other "defining things").
  845.      (list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
  846.            2 'font-lock-function-name-face)
  847.  
  848.      ;; Names of type-defining things.
  849.      (list (concat "\\(\\s-\\|^\\)\\("
  850.                ;; FIXME Use 'regexp-quote?
  851.                (mapconcat 'identity tcl-typeword-list "\\|")
  852.                "\\)\\(\\s-\\|$\\)")
  853.            2 'font-lock-type-face)
  854.  
  855.      ;; Keywords.  Only recognized if surrounded by whitespace.
  856.      ;; FIXME consider using "not word or symbol", not
  857.      ;; "whitespace".
  858.      (cons (concat "\\(\\s-\\|^\\)\\("
  859.                ;; FIXME Use regexp-quote? 
  860.                (mapconcat 'identity tcl-keyword-list "\\|")
  861.                "\\)\\(\\s-\\|$\\)")
  862.            2)
  863.      )))
  864.  
  865. (if tcl-proc-regexp
  866.     ()
  867.   (tcl-set-proc-regexp))
  868.  
  869. (if tcl-font-lock-keywords
  870.     ()
  871.   (tcl-set-font-lock-keywords))
  872.  
  873.  
  874.  
  875. ;;
  876. ;; The mode itself.
  877. ;;
  878.  
  879. ;;;###autoload
  880. (defun tcl-mode ()
  881.   "Major mode for editing Tcl code.
  882. Expression and list commands understand all Tcl brackets.
  883. Tab indents for Tcl code.
  884. Paragraphs are separated by blank lines only.
  885. Delete converts tabs to spaces as it moves back.
  886.  
  887. Variables controlling indentation style:
  888.   tcl-indent-level
  889.     Indentation of Tcl statements within surrounding block.
  890.   tcl-continued-indent-level
  891.     Indentation of continuation line relative to first line of command.
  892.  
  893. Variables controlling user interaction with mode (see variable
  894. documentation for details):
  895.   tcl-tab-always-indent
  896.     Controls action of TAB key.
  897.   tcl-auto-newline
  898.     Non-nil means automatically newline before and after braces, brackets,
  899.     and semicolons inserted in Tcl code.
  900.   tcl-electric-hash-style
  901.     Controls action of `#' key.
  902.   tcl-use-hairy-comment-detector
  903.     If t, use more complicated, but slower, comment detector.
  904.     This variable is only used in GNU Emacs 19.
  905.  
  906. Turning on Tcl mode calls the value of the variable `tcl-mode-hook'
  907. with no args, if that value is non-nil.  Read the documentation for
  908. `tcl-mode-hook' to see what kinds of interesting hook functions
  909. already exist.
  910.  
  911. Commands:
  912. \\{tcl-mode-map}"
  913.   (interactive)
  914.   (kill-all-local-variables)
  915.   (use-local-map tcl-mode-map)
  916.   (setq major-mode 'tcl-mode)
  917.   (setq mode-name "Tcl")
  918.   (setq local-abbrev-table tcl-mode-abbrev-table)
  919.   (set-syntax-table tcl-mode-syntax-table)
  920.  
  921.   (make-local-variable 'paragraph-start)
  922.   (make-local-variable 'paragraph-separate)
  923.   (if (and tcl-using-emacs-19-23
  924.        (>= emacs-minor-version 29))
  925.       (progn
  926.     ;; In Emacs 19.29, you aren't supposed to start these with a
  927.     ;; ^.
  928.     (setq paragraph-start "$\\| ")
  929.     (setq paragraph-separate paragraph-start))
  930.     (setq paragraph-start (concat "^$\\|" page-delimiter))
  931.     (setq paragraph-separate paragraph-start))
  932.   (make-local-variable 'paragraph-ignore-fill-prefix)
  933.   (setq paragraph-ignore-fill-prefix t)
  934.   (make-local-variable 'fill-paragraph-function)
  935.   (setq fill-paragraph-function 'tcl-do-fill-paragraph)
  936.  
  937.   (make-local-variable 'indent-line-function)
  938.   (setq indent-line-function 'tcl-indent-line)
  939.   ;; Tcl doesn't require a final newline.
  940.   ;; (make-local-variable 'require-final-newline)
  941.   ;; (setq require-final-newline t)
  942.  
  943.   (make-local-variable 'comment-start)
  944.   (setq comment-start "# ")
  945.   (make-local-variable 'comment-start-skip)
  946.   (setq comment-start-skip "#+ *")
  947.   (make-local-variable 'comment-column)
  948.   (setq comment-column 40)
  949.   (make-local-variable 'comment-end)
  950.   (setq comment-end "")
  951.  
  952.   (make-local-variable 'outline-regexp)
  953.   (setq outline-regexp "[^\n\^M]")
  954.   (make-local-variable 'outline-level)
  955.   (setq outline-level 'tcl-outline-level)
  956.  
  957.   (make-local-variable 'font-lock-keywords)
  958.   (setq font-lock-keywords tcl-font-lock-keywords)
  959.  
  960.   ;; The following only really makes sense under GNU Emacs 19.
  961.   (make-local-variable 'imenu-create-index-function)
  962.   (setq imenu-create-index-function 'tcl-imenu-create-index-function)
  963.   (make-local-variable 'parse-sexp-ignore-comments)
  964.  
  965.   ;; Settings for new dabbrev code.
  966.   (make-local-variable 'dabbrev-case-fold-search)
  967.   (setq dabbrev-case-fold-search nil)
  968.   (make-local-variable 'dabbrev-case-replace)
  969.   (setq dabbrev-case-replace nil)
  970.   (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
  971.   (setq dabbrev-abbrev-skip-leading-regexp "[$!]")
  972.   (make-local-variable 'dabbrev-abbrev-char-regexp)
  973.   (setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_")
  974.  
  975.   (if tcl-using-emacs-19
  976.       (progn
  977.     ;; This can only be set to t in Emacs 19 and XEmacs.
  978.     ;; Emacs 18 and Epoch lose.
  979.     (setq parse-sexp-ignore-comments t)
  980.     ;; XEmacs has defun-prompt-regexp, but I don't believe
  981.     ;; that it works for end-of-defun -- only for
  982.     ;; beginning-of-defun.
  983.     (make-local-variable 'defun-prompt-regexp)
  984.     (setq defun-prompt-regexp tcl-omit-ws-regexp)
  985.     ;; The following doesn't work in Lucid Emacs 19.6, but maybe
  986.     ;; it will appear in later versions.
  987.     (make-local-variable 'add-log-current-defun-function)
  988.     (setq add-log-current-defun-function 'add-log-tcl-defun))
  989.     (setq parse-sexp-ignore-comments nil))
  990.  
  991.   ;; Put Tcl menu into menubar for XEmacs.  This happens
  992.   ;; automatically for GNU Emacs.
  993.   (if (and tcl-using-xemacs-19
  994.        current-menubar
  995.        (not (assoc "Tcl" current-menubar)))
  996.       (progn
  997.     (set-buffer-menubar (copy-sequence current-menubar))
  998.     (add-menu nil "Tcl" tcl-xemacs-menu)))
  999.   ;; Append Tcl menu to popup menu for XEmacs.
  1000.   (if (and tcl-using-xemacs-19 (boundp 'mode-popup-menu))
  1001.       (setq mode-popup-menu
  1002.         (cons (concat mode-name " Mode Commands") tcl-xemacs-menu)))
  1003.  
  1004.   (run-hooks 'tcl-mode-hook))
  1005.  
  1006.  
  1007.  
  1008. ;; This is used for braces, brackets, and semi (except for closing
  1009. ;; braces, which are handled specially).
  1010. (defun tcl-electric-char (arg)
  1011.   "Insert character and correct line's indentation."
  1012.   (interactive "p")
  1013.   ;; Indent line first; this looks better if parens blink.
  1014.   (tcl-indent-line)
  1015.   (self-insert-command arg)
  1016.   (if (and tcl-auto-newline (= last-command-char ?\;))
  1017.       (progn
  1018.     (newline)
  1019.     (tcl-indent-line))))
  1020.  
  1021. ;; This is used for closing braces.  If tcl-auto-newline is set, can
  1022. ;; insert a newline both before and after the brace, depending on
  1023. ;; context.  FIXME should this be configurable?  Does anyone use this?
  1024. (defun tcl-electric-brace (arg)
  1025.   "Insert character and correct line's indentation."
  1026.   (interactive "p")
  1027.   ;; If auto-newlining and there is stuff on the same line, insert a
  1028.   ;; newline first.
  1029.   (if tcl-auto-newline
  1030.       (progn
  1031.     (if (save-excursion
  1032.           (skip-chars-backward " \t")
  1033.           (bolp))
  1034.         ()
  1035.       (tcl-indent-line)
  1036.       (newline))
  1037.     ;; In auto-newline case, must insert a newline after each
  1038.     ;; brace.  So an explicit loop is needed.
  1039.     (while (> arg 0)
  1040.       (insert last-command-char)
  1041.       (tcl-indent-line)
  1042.       (newline)
  1043.       (setq arg (1- arg))))
  1044.     (self-insert-command arg))
  1045.   (tcl-indent-line))
  1046.  
  1047.  
  1048.  
  1049. (defun tcl-indent-command (&optional arg)
  1050.   "Indent current line as Tcl code, or in some cases insert a tab character.
  1051. If tcl-tab-always-indent is t (the default), always indent current line.
  1052. If tcl-tab-always-indent is nil and point is not in the indentation
  1053. area at the beginning of the line, a TAB is inserted.
  1054. Other values of tcl-tab-always-indent cause the first possible action
  1055. from the following list to take place:
  1056.  
  1057.   1. Move from beginning of line to correct indentation.
  1058.   2. Delete an empty comment.
  1059.   3. Move forward to start of comment, indenting if necessary.
  1060.   4. Move forward to end of line, indenting if necessary.
  1061.   5. Create an empty comment.
  1062.   6. Move backward to start of comment, indenting if necessary."
  1063.   (interactive "p")
  1064.   (cond
  1065.    ((not tcl-tab-always-indent)
  1066.     ;; Indent if in indentation area, otherwise insert TAB.
  1067.     (if (<= (current-column) (current-indentation))
  1068.     (tcl-indent-line)
  1069.       (self-insert-command arg)))
  1070.    ((eq tcl-tab-always-indent t)
  1071.     ;; Always indent.
  1072.     (tcl-indent-line))
  1073.    (t
  1074.     ;; "Perl-mode" style TAB command.
  1075.     (let* ((ipoint (point))
  1076.        (eolpoint (progn
  1077.                (end-of-line)
  1078.                (point)))
  1079.        (comment-p (tcl-in-comment)))
  1080.       (cond
  1081.        ((= ipoint (save-excursion
  1082.             (beginning-of-line)
  1083.             (point)))
  1084.     (beginning-of-line)
  1085.     (tcl-indent-line)
  1086.     ;; If indenting didn't leave us in column 0, go to the
  1087.     ;; indentation.  Otherwise leave point at end of line.  This
  1088.     ;; is a hack.
  1089.     (if (= (point) (save-excursion
  1090.              (beginning-of-line)
  1091.              (point)))
  1092.         (end-of-line)
  1093.       (back-to-indentation)))
  1094.        ((and comment-p (looking-at "[ \t]*$"))
  1095.     ;; Empty comment, so delete it.  We also delete any ";"
  1096.     ;; characters at the end of the line.  I think this is
  1097.     ;; friendlier, but I don't know how other people will feel.
  1098.     (backward-char)
  1099.     (skip-chars-backward " \t;")
  1100.     (delete-region (point) eolpoint))
  1101.        ((and comment-p (< ipoint (point)))
  1102.     ;; Before comment, so skip to it.
  1103.     (tcl-indent-line)
  1104.     (indent-for-comment))
  1105.        ((/= ipoint eolpoint)
  1106.     ;; Go to end of line (since we're not there yet).
  1107.     (goto-char eolpoint)
  1108.     (tcl-indent-line))
  1109.        ((not comment-p)
  1110.     (tcl-indent-line)
  1111.     (tcl-indent-for-comment))
  1112.        (t
  1113.     ;; Go to start of comment.  We don't leave point where it is
  1114.     ;; because we want to skip comment-start-skip.
  1115.     (tcl-indent-line)
  1116.     (indent-for-comment)))))))
  1117.  
  1118. (defun tcl-indent-line ()
  1119.   "Indent current line as Tcl code.
  1120. Return the amount the indentation changed by."
  1121.   (let ((indent (calculate-tcl-indent nil))
  1122.     beg shift-amt
  1123.     (case-fold-search nil)
  1124.     (pos (- (point-max) (point))))
  1125.     (beginning-of-line)
  1126.     (setq beg (point))
  1127.     (cond ((eq indent nil)
  1128.        (setq indent (current-indentation)))
  1129.       (t
  1130.        (skip-chars-forward " \t")
  1131.        (if (listp indent) (setq indent (car indent)))
  1132.        (cond ((= (following-char) ?})
  1133.           (setq indent (- indent tcl-indent-level)))
  1134.          ((= (following-char) ?\])
  1135.           (setq indent (- indent 1))))))
  1136.     (skip-chars-forward " \t")
  1137.     (setq shift-amt (- indent (current-column)))
  1138.     (if (zerop shift-amt)
  1139.     (if (> (- (point-max) pos) (point))
  1140.         (goto-char (- (point-max) pos)))
  1141.       (delete-region beg (point))
  1142.       (indent-to indent)
  1143.       ;; If initial point was within line's indentation,
  1144.       ;; position after the indentation.  Else stay at same point in text.
  1145.       (if (> (- (point-max) pos) (point))
  1146.       (goto-char (- (point-max) pos))))
  1147.     shift-amt))
  1148.  
  1149. (defun tcl-figure-type ()
  1150.   "Determine type of sexp at point.
  1151. This is either 'tcl-expr, 'tcl-commands, or nil.  Puts point at start
  1152. of sexp that indicates types.
  1153.  
  1154. See documentation for variable `tcl-type-alist' for more information."
  1155.   (let ((count 0)
  1156.     result
  1157.     word-stack)
  1158.     (while (and (< count 5)
  1159.         (not result))
  1160.       (condition-case nil
  1161.       (progn
  1162.         ;; FIXME should use "tcl-backward-sexp", which would skip
  1163.         ;; over entire variables, etc.
  1164.         (backward-sexp)
  1165.         (if (looking-at "[a-zA-Z_]+")
  1166.         (let ((list tcl-type-alist)
  1167.               entry)
  1168.           (setq word-stack (cons (current-word) word-stack))
  1169.           (while (and list (not result))
  1170.             (setq entry (car list))
  1171.             (setq list (cdr list))
  1172.             (let ((index 0))
  1173.               (while (and entry (<= index count))
  1174.             ;; Abort loop if string does not match word on
  1175.             ;; stack.
  1176.             (and (stringp (car entry))
  1177.                  (not (string= (car entry)
  1178.                        (nth index word-stack)))
  1179.                  (setq entry nil))
  1180.             (setq entry (cdr entry))
  1181.             (setq index (1+ index)))
  1182.               (and (> index count)
  1183.                (not (stringp (car entry)))
  1184.                (setq result (car entry)))
  1185.               )))
  1186.           (setq word-stack (cons nil word-stack))))
  1187.     (error nil))
  1188.       (setq count (1+ count)))
  1189.     (and tcl-explain-indentation
  1190.      (message "Indentation type %s" result))
  1191.     result))
  1192.  
  1193. (defun calculate-tcl-indent (&optional parse-start)
  1194.   "Return appropriate indentation for current line as Tcl code.
  1195. In usual case returns an integer: the column to indent to.
  1196. Returns nil if line starts inside a string, t if in a comment."
  1197.   (save-excursion
  1198.     (beginning-of-line)
  1199.     (let* ((indent-point (point))
  1200.        (case-fold-search nil)
  1201.        (continued-line 
  1202.         (save-excursion
  1203.           (if (bobp)
  1204.           nil
  1205.         (backward-char)
  1206.         (= ?\\ (preceding-char)))))
  1207.        (continued-indent-value (if continued-line
  1208.                        tcl-continued-indent-level
  1209.                      0))
  1210.        state
  1211.        containing-sexp
  1212.        found-next-line)
  1213.       (if parse-start
  1214.       (goto-char parse-start)
  1215.     (tcl-beginning-of-defun))
  1216.       (while (< (point) indent-point)
  1217.     (setq parse-start (point))
  1218.     (setq state (parse-partial-sexp (point) indent-point 0))
  1219.     (setq containing-sexp (car (cdr state))))
  1220.       (cond ((or (nth 3 state) (nth 4 state))
  1221.          ;; Inside comment or string.  Return nil or t if should
  1222.          ;; not change this line
  1223.          (nth 4 state))
  1224.         ((null containing-sexp)
  1225.          ;; Line is at top level.
  1226.          continued-indent-value)
  1227.         (t
  1228.          ;; Set expr-p if we are looking at the expression part of
  1229.          ;; an "if", "expr", etc statement.  Set commands-p if we
  1230.          ;; are looking at the body part of an if, while, etc
  1231.          ;; statement.  FIXME Should check for "for" loops here.
  1232.          (goto-char containing-sexp)
  1233.          (let* ((sexpr-type (tcl-figure-type))
  1234.             (expr-p (eq sexpr-type 'tcl-expr))
  1235.             (commands-p (eq sexpr-type 'tcl-commands))
  1236.             (expr-start (point)))
  1237.            ;; Find the first statement in the block and indent
  1238.            ;; like it.  The first statement in the block might be
  1239.            ;; on the same line, so what we do is skip all
  1240.            ;; "virtually blank" lines, looking for a non-blank
  1241.            ;; one.  A line is virtually blank if it only contains
  1242.            ;; a comment and whitespace.  FIXME continued comments
  1243.            ;; aren't supported.  They are a wart on Tcl anyway.
  1244.            ;; We do it this funky way because we want to know if
  1245.            ;; we've found a statement on some line _after_ the
  1246.            ;; line holding the sexp opener.
  1247.            (goto-char containing-sexp)
  1248.            (forward-char)
  1249.            (if (and (< (point) indent-point)
  1250.             (looking-at "[ \t]*\\(#.*\\)?$"))
  1251.            (progn
  1252.              (forward-line)
  1253.              (while (and (< (point) indent-point)
  1254.                  (looking-at "[ \t]*\\(#.*\\)?$"))
  1255.                (setq found-next-line t)
  1256.                (forward-line))))
  1257.            (if (or continued-line
  1258.                (/= (char-after containing-sexp) ?{)
  1259.                expr-p)
  1260.            (progn
  1261.              ;; Line is continuation line, or the sexp opener
  1262.              ;; is not a curly brace, or we are are looking at
  1263.              ;; an `expr' expression (which must be split
  1264.              ;; specially).  So indentation is column of first
  1265.              ;; good spot after sexp opener (with some added
  1266.              ;; in the continued-line case).  If there is no
  1267.              ;; nonempty line before the indentation point, we
  1268.              ;; use the column of the character after the sexp
  1269.              ;; opener.
  1270.              (if (>= (point) indent-point)
  1271.              (progn
  1272.                (goto-char containing-sexp)
  1273.                (forward-char))
  1274.                (skip-chars-forward " \t"))
  1275.              (+ (current-column) continued-indent-value))
  1276.          ;; After a curly brace, and not a continuation line.
  1277.          ;; So take indentation from first good line after
  1278.          ;; start of block, unless that line is on the same
  1279.          ;; line as the opening brace.  In this case use the
  1280.          ;; indentation of the opening brace's line, plus
  1281.          ;; another indent step.  If we are in the body part
  1282.          ;; of an "if" or "while" then the indentation is
  1283.          ;; taken from the line holding the start of the
  1284.          ;; statement.
  1285.          (if (and (< (point) indent-point)
  1286.               found-next-line)
  1287.              (current-indentation)
  1288.            (if commands-p
  1289.                (goto-char expr-start)
  1290.              (goto-char containing-sexp))
  1291.            (+ (current-indentation) tcl-indent-level)))))))))
  1292.  
  1293.  
  1294.  
  1295. (defun indent-tcl-exp ()
  1296.   "Indent each line of the Tcl grouping following point."
  1297.   (interactive)
  1298.   (let ((indent-stack (list nil))
  1299.     (contain-stack (list (point)))
  1300.     (case-fold-search nil)
  1301.     outer-loop-done inner-loop-done state ostate
  1302.     this-indent last-sexp continued-line
  1303.     (next-depth 0)
  1304.     last-depth)
  1305.     (save-excursion
  1306.       (forward-sexp 1))
  1307.     (save-excursion
  1308.       (setq outer-loop-done nil)
  1309.       (while (and (not (eobp)) (not outer-loop-done))
  1310.     (setq last-depth next-depth)
  1311.     ;; Compute how depth changes over this line
  1312.     ;; plus enough other lines to get to one that
  1313.     ;; does not end inside a comment or string.
  1314.     ;; Meanwhile, do appropriate indentation on comment lines.
  1315.     (setq inner-loop-done nil)
  1316.     (while (and (not inner-loop-done)
  1317.             (not (and (eobp) (setq outer-loop-done t))))
  1318.       (setq ostate state)
  1319.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  1320.                       nil nil state))
  1321.       (setq next-depth (car state))
  1322.       (if (and (car (cdr (cdr state)))
  1323.            (>= (car (cdr (cdr state))) 0))
  1324.           (setq last-sexp (car (cdr (cdr state)))))
  1325.       (if (or (nth 4 ostate))
  1326.           (tcl-indent-line))
  1327.       (if (or (nth 3 state))
  1328.           (forward-line 1)
  1329.         (setq inner-loop-done t)))
  1330.     (if (<= next-depth 0)
  1331.         (setq outer-loop-done t))
  1332.     (if outer-loop-done
  1333.         nil
  1334.       ;; If this line had ..))) (((.. in it, pop out of the levels
  1335.       ;; that ended anywhere in this line, even if the final depth
  1336.       ;; doesn't indicate that they ended.
  1337.       (while (> last-depth (nth 6 state))
  1338.         (setq indent-stack (cdr indent-stack)
  1339.           contain-stack (cdr contain-stack)
  1340.           last-depth (1- last-depth)))
  1341.       (if (/= last-depth next-depth)
  1342.           (setq last-sexp nil))
  1343.       ;; Add levels for any parens that were started in this line.
  1344.       (while (< last-depth next-depth)
  1345.         (setq indent-stack (cons nil indent-stack)
  1346.           contain-stack (cons nil contain-stack)
  1347.           last-depth (1+ last-depth)))
  1348.       (if (null (car contain-stack))
  1349.           (setcar contain-stack 
  1350.               (or (car (cdr state))
  1351.               (save-excursion
  1352.                 (forward-sexp -1)
  1353.                 (point)))))
  1354.       (forward-line 1)
  1355.       (setq continued-line 
  1356.         (save-excursion
  1357.           (backward-char)
  1358.           (= (preceding-char) ?\\)))
  1359.       (skip-chars-forward " \t")
  1360.       (if (eolp)
  1361.           nil
  1362.         (if (and (car indent-stack)
  1363.              (>= (car indent-stack) 0))
  1364.         ;; Line is on an existing nesting level.
  1365.         (setq this-indent (car indent-stack))
  1366.           ;; Just started a new nesting level.
  1367.           ;; Compute the standard indent for this level.
  1368.           (let ((val (calculate-tcl-indent
  1369.               (if (car indent-stack)
  1370.                   (- (car indent-stack))))))
  1371.         (setcar indent-stack
  1372.             (setq this-indent val))
  1373.         (setq continued-line nil)))
  1374.         (cond ((not (numberp this-indent)))
  1375.           ((= (following-char) ?})
  1376.            (setq this-indent (- this-indent tcl-indent-level)))
  1377.           ((= (following-char) ?\])
  1378.            (setq this-indent (- this-indent 1))))
  1379.         ;; Put chosen indentation into effect.
  1380.         (or (null this-indent)
  1381.         (= (current-column) 
  1382.            (if continued-line 
  1383.                (+ this-indent tcl-indent-level)
  1384.              this-indent))
  1385.         (progn
  1386.           (delete-region (point) (progn (beginning-of-line) (point)))
  1387.           (indent-to 
  1388.            (if continued-line 
  1389.                (+ this-indent tcl-indent-level)
  1390.              this-indent)))))))))
  1391.   )
  1392.  
  1393.  
  1394.  
  1395. ;;
  1396. ;; Interfaces to other packages.
  1397. ;;
  1398.  
  1399. (defun tcl-imenu-create-index-function ()
  1400.   "Generate alist of indices for imenu."
  1401.   (let ((re (concat tcl-proc-regexp "\\([^ \t\n{]+\\)"))
  1402.     alist prev-pos)
  1403.     (goto-char (point-min))
  1404.     (imenu-progress-message prev-pos 0)
  1405.     (save-match-data
  1406.       (while (re-search-forward re nil t)
  1407.     (imenu-progress-message prev-pos)
  1408.     ;; Position on start of proc name, not beginning of line.
  1409.     (setq alist (cons
  1410.              (cons (buffer-substring (match-beginning 2) (match-end 2))
  1411.                (match-beginning 2))
  1412.              alist))))
  1413.     (imenu-progress-message prev-pos 100)
  1414.     (nreverse alist)))
  1415.  
  1416. ;; FIXME Definition of function is very ad-hoc.  Should use
  1417. ;; tcl-beginning-of-defun.  Also has incestuous knowledge about the
  1418. ;; format of tcl-proc-regexp.
  1419. (defun add-log-tcl-defun ()
  1420.   "Return name of Tcl function point is in, or nil."
  1421.   (save-excursion
  1422.     (if (re-search-backward
  1423.      (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
  1424.     (buffer-substring (match-beginning 2)
  1425.               (match-end 2)))))
  1426.  
  1427. (defun tcl-outline-level ()
  1428.   (save-excursion
  1429.     (skip-chars-forward " \t")
  1430.     (current-column)))
  1431.  
  1432.  
  1433.  
  1434. ;;
  1435. ;; Helper functions for inferior Tcl mode.
  1436. ;;
  1437.  
  1438. ;; This exists to let us delete the prompt when commands are sent
  1439. ;; directly to the inferior Tcl.  See gud.el for an explanation of how
  1440. ;; it all works (I took it from there).  This stuff doesn't really
  1441. ;; work as well as I'd like it to.  But I don't believe there is
  1442. ;; anything useful that can be done.
  1443. (defvar inferior-tcl-delete-prompt-marker nil)
  1444.  
  1445. (defun tcl-filter (proc string)
  1446.   (let ((inhibit-quit t))
  1447.     (save-excursion
  1448.       (set-buffer (process-buffer proc))
  1449.       (goto-char (process-mark proc))
  1450.       ;; Delete prompt if requested.
  1451.       (if (marker-buffer inferior-tcl-delete-prompt-marker)
  1452.       (progn
  1453.         (delete-region (point) inferior-tcl-delete-prompt-marker)
  1454.         (set-marker inferior-tcl-delete-prompt-marker nil)))))
  1455.   (if tcl-using-emacs-19
  1456.       (comint-output-filter proc string)
  1457.     (funcall comint-output-filter string)))
  1458.  
  1459. (defun tcl-send-string (proc string)
  1460.   (save-excursion
  1461.     (set-buffer (process-buffer proc))
  1462.     (goto-char (process-mark proc))
  1463.     (beginning-of-line)
  1464.     (if (looking-at comint-prompt-regexp)
  1465.     (set-marker inferior-tcl-delete-prompt-marker (point))))
  1466.   (comint-send-string proc string))
  1467.  
  1468. (defun tcl-send-region (proc start end)
  1469.   (save-excursion
  1470.     (set-buffer (process-buffer proc))
  1471.     (goto-char (process-mark proc))
  1472.     (beginning-of-line)
  1473.     (if (looking-at comint-prompt-regexp)
  1474.     (set-marker inferior-tcl-delete-prompt-marker (point))))
  1475.   (comint-send-region proc start end))
  1476.  
  1477. (defun switch-to-tcl (eob-p)
  1478.   "Switch to inferior Tcl process buffer.
  1479. With argument, positions cursor at end of buffer."
  1480.   (interactive "P")
  1481.   (if (get-buffer inferior-tcl-buffer)
  1482.       (pop-to-buffer inferior-tcl-buffer)
  1483.     (error "No current inferior Tcl buffer"))
  1484.   (cond (eob-p
  1485.      (push-mark)
  1486.      (goto-char (point-max)))))
  1487.  
  1488. (defun inferior-tcl-proc ()
  1489.   "Return current inferior Tcl process.
  1490. See variable `inferior-tcl-buffer'."
  1491.   (let ((proc (get-buffer-process (if (eq major-mode 'inferior-tcl-mode)
  1492.                       (current-buffer)
  1493.                     inferior-tcl-buffer))))
  1494.     (or proc
  1495.     (error "No Tcl process; see variable `inferior-tcl-buffer'"))))
  1496.  
  1497. (defun tcl-eval-region (start end &optional and-go)
  1498.   "Send the current region to the inferior Tcl process.
  1499. Prefix argument means switch to the Tcl buffer afterwards."
  1500.   (interactive "r\nP")
  1501.   (let ((proc (inferior-tcl-proc)))
  1502.     (tcl-send-region proc start end)
  1503.     (tcl-send-string proc "\n")
  1504.     (if and-go (switch-to-tcl t))))
  1505.  
  1506. (defun tcl-eval-defun (&optional and-go)
  1507.   "Send the current defun to the inferior Tcl process.
  1508. Prefix argument means switch to the Tcl buffer afterwards."
  1509.   (interactive "P")
  1510.   (save-excursion
  1511.     (tcl-end-of-defun)
  1512.     (let ((end (point)))
  1513.       (tcl-beginning-of-defun)
  1514.       (tcl-eval-region (point) end)))
  1515.   (if and-go (switch-to-tcl t)))
  1516.  
  1517.  
  1518.  
  1519. ;;
  1520. ;; Inferior Tcl mode itself.
  1521. ;;
  1522.  
  1523. (defun inferior-tcl-mode ()
  1524.   "Major mode for interacting with Tcl interpreter.
  1525.  
  1526. A Tcl process can be started with M-x inferior-tcl.
  1527.  
  1528. Entry to this mode runs the hooks comint-mode-hook and
  1529. inferior-tcl-mode-hook, in that order.
  1530.  
  1531. You can send text to the inferior Tcl process from other buffers
  1532. containing Tcl source.
  1533.  
  1534. Variables controlling Inferior Tcl mode:
  1535.   tcl-application
  1536.     Name of program to run.
  1537.   tcl-command-switches
  1538.     Command line arguments to `tcl-application'.
  1539.   tcl-prompt-regexp
  1540.     Matches prompt.
  1541.   inferior-tcl-source-command
  1542.     Command to use to read Tcl file in running application.
  1543.   inferior-tcl-buffer
  1544.     The current inferior Tcl process buffer.  See variable
  1545.     documentation for details on multiple-process support.
  1546.  
  1547. The following commands are available:
  1548. \\{inferior-tcl-mode-map}"
  1549.   (interactive)
  1550.   (comint-mode)
  1551.   (setq comint-prompt-regexp (or tcl-prompt-regexp
  1552.                  (concat "^"
  1553.                      (regexp-quote tcl-application)
  1554.                      ">")))
  1555.   (setq major-mode 'inferior-tcl-mode)
  1556.   (setq mode-name "Inferior Tcl")
  1557.   (if (boundp 'modeline-process)
  1558.       (setq modeline-process '(": %s"))    ; For XEmacs.
  1559.     (setq mode-line-process '(": %s")))
  1560.   (use-local-map inferior-tcl-mode-map)
  1561.   (setq local-abbrev-table tcl-mode-abbrev-table)
  1562.   (set-syntax-table tcl-mode-syntax-table)
  1563.   (if tcl-using-emacs-19
  1564.       (progn
  1565.     (make-local-variable 'defun-prompt-regexp)
  1566.     (setq defun-prompt-regexp tcl-omit-ws-regexp)))
  1567.   (make-local-variable 'inferior-tcl-delete-prompt-marker)
  1568.   (setq inferior-tcl-delete-prompt-marker (make-marker))
  1569.   (set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter)
  1570.   (run-hooks 'inferior-tcl-mode-hook))
  1571.  
  1572. ;;;###autoload
  1573. (defun inferior-tcl (cmd)
  1574.   "Run inferior Tcl process.
  1575. Prefix arg means enter program name interactively.
  1576. See documentation for function `inferior-tcl-mode' for more information."
  1577.   (interactive
  1578.    (list (if current-prefix-arg
  1579.          (read-string "Run Tcl: " tcl-application)
  1580.        tcl-application)))
  1581.   (if (not (comint-check-proc "*inferior-tcl*"))
  1582.       (progn
  1583.     (set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
  1584.                tcl-command-switches))
  1585.     (inferior-tcl-mode)))
  1586.   (make-local-variable 'tcl-application)
  1587.   (setq tcl-application cmd)
  1588.   (setq inferior-tcl-buffer "*inferior-tcl*")
  1589.   (switch-to-buffer "*inferior-tcl*"))
  1590.  
  1591. (and (fboundp 'defalias)
  1592.      (defalias 'run-tcl 'inferior-tcl))
  1593.  
  1594.  
  1595.  
  1596. ;;
  1597. ;; Auto-fill support.
  1598. ;;
  1599.  
  1600. (defun tcl-real-command-p ()
  1601.   "Return nil if point is not at the beginning of a command.
  1602. A command is the first word on an otherwise empty line, or the
  1603. first word following a semicolon, opening brace, or opening bracket."
  1604.   (save-excursion
  1605.     (skip-chars-backward " \t")
  1606.     (cond
  1607.      ((bobp) t)
  1608.      ((bolp)
  1609.       (backward-char)
  1610.       ;; Note -- continued comments are not supported here.  I
  1611.       ;; consider those to be a wart on the language.
  1612.       (not (eq ?\\ (preceding-char))))
  1613.      (t
  1614.       (memq (preceding-char) '(?\; ?{ ?\[))))))
  1615.  
  1616. ;; FIXME doesn't actually return t.  See last case.
  1617. (defun tcl-real-comment-p ()
  1618.   "Return t if point is just after the `#' beginning a real comment.
  1619. Does not check to see if previous char is actually `#'.
  1620. A real comment is either at the beginning of the buffer,
  1621. preceeded only by whitespace on the line, or has a preceeding
  1622. semicolon, opening brace, or opening bracket on the same line."
  1623.   (save-excursion
  1624.     (backward-char)
  1625.     (tcl-real-command-p)))
  1626.  
  1627. (defun tcl-hairy-scan-for-comment (state end always-stop)
  1628.   "Determine if point is in a comment.
  1629. Returns a list of the form `(FLAG . STATE)'.  STATE can be used
  1630. as input to future invocations.  FLAG is nil if not in comment,
  1631. t otherwise.  If in comment, leaves point at beginning of comment.
  1632. Only works in Emacs 19.  See also `tcl-simple-scan-for-comment', a
  1633. simpler version that is often right, and works in Emacs 18."
  1634.   (let ((bol (save-excursion
  1635.            (goto-char end)
  1636.            (beginning-of-line)
  1637.            (point)))
  1638.     real-comment
  1639.     last-cstart)
  1640.     (while (and (not last-cstart) (< (point) end))
  1641.       (setq real-comment nil)        ;In case we've looped around and it is
  1642.                                         ;set.
  1643.       (setq state (parse-partial-sexp (point) end nil nil state t))
  1644.       (if (nth 4 state)
  1645.       (progn
  1646.         ;; If ALWAYS-STOP is set, stop even if we don't have a
  1647.         ;; real comment, or if the comment isn't on the same line
  1648.         ;; as the end.
  1649.         (if always-stop (setq last-cstart (point)))
  1650.         ;; If we have a real comment, then set the comment
  1651.         ;; starting point if we are on the same line as the ending
  1652.         ;; location.
  1653.         (setq real-comment (tcl-real-comment-p))
  1654.         (if real-comment
  1655.         (progn
  1656.           (and (> (point) bol) (setq last-cstart (point)))
  1657.           ;; NOTE Emacs 19 has a misfeature whereby calling
  1658.           ;; parse-partial-sexp with COMMENTSTOP set and with
  1659.           ;; an initial list that says point is in a comment
  1660.           ;; will cause an immediate return.  So we must skip
  1661.           ;; over the comment ourselves.
  1662.           (beginning-of-line 2)))
  1663.         ;; Frob the state to make it look like we aren't in a
  1664.         ;; comment.
  1665.         (setcar (nthcdr 4 state) nil))))
  1666.     (and last-cstart
  1667.      (goto-char last-cstart))
  1668.     (cons real-comment state)))
  1669.  
  1670. (defun tcl-hairy-in-comment ()
  1671.   "Return t if point is in a comment, and leave point at beginning
  1672. of comment."
  1673.   (let ((save (point)))
  1674.     (tcl-beginning-of-defun)
  1675.     (car (tcl-hairy-scan-for-comment nil save nil))))
  1676.  
  1677. (defun tcl-simple-in-comment ()
  1678.   "Return t if point is in comment, and leave point at beginning
  1679. of comment.  This is faster that `tcl-hairy-in-comment', but is
  1680. correct less often."
  1681.   (let ((save (point))
  1682.     comment)
  1683.     (beginning-of-line)
  1684.     (while (and (< (point) save) (not comment))
  1685.       (search-forward "#" save 'move)
  1686.       (setq comment (tcl-real-comment-p)))
  1687.     comment))
  1688.  
  1689. (defun tcl-in-comment ()
  1690.   "Return t if point is in comment, and leave point at beginning
  1691. of comment."
  1692.   (if (and tcl-pps-has-arg-6
  1693.        tcl-use-hairy-comment-detector)
  1694.       (tcl-hairy-in-comment)
  1695.     (tcl-simple-in-comment)))
  1696.  
  1697. (defun tcl-do-fill-paragraph (ignore)
  1698.   "fill-paragraph function for Tcl mode.  Only fills in a comment."
  1699.   (let (in-comment col where)
  1700.     (save-excursion
  1701.       (end-of-line)
  1702.       (setq in-comment (tcl-in-comment))
  1703.       (if in-comment
  1704.       (progn
  1705.         (setq where (1+ (point)))
  1706.         (setq col (1- (current-column))))))
  1707.     (and in-comment
  1708.      (save-excursion
  1709.        (back-to-indentation)
  1710.        (= col (current-column)))
  1711.      ;; In a comment.  Set the fill prefix, and find the paragraph
  1712.      ;; boundaries by searching for lines that look like
  1713.      ;; comment-only lines.
  1714.      (let ((fill-prefix (buffer-substring (progn
  1715.                         (beginning-of-line)
  1716.                         (point))
  1717.                           where))
  1718.            p-start p-end)
  1719.        ;; Search backwards.
  1720.        (save-excursion
  1721.          (while (looking-at "^[ \t]*#")
  1722.            (forward-line -1))
  1723.          (forward-line)
  1724.          (setq p-start (point)))
  1725.  
  1726.        ;; Search forwards.
  1727.        (save-excursion
  1728.          (while (looking-at "^[ \t]*#")
  1729.            (forward-line))
  1730.          (setq p-end (point)))
  1731.  
  1732.        ;; Narrow and do the fill.
  1733.        (save-restriction
  1734.          (narrow-to-region p-start p-end)
  1735.          (fill-paragraph ignore)))))
  1736.   t)
  1737.  
  1738. (defun tcl-do-auto-fill ()
  1739.   "Auto-fill function for Tcl mode.  Only auto-fills in a comment."
  1740.   (let ((fill-prefix "# ")
  1741.     in-comment col)
  1742.     (save-excursion
  1743.       (setq in-comment (tcl-in-comment))
  1744.       (if in-comment
  1745.       (setq col (1- (current-column)))))
  1746.     (if in-comment
  1747.     (progn
  1748.       (do-auto-fill)
  1749.       (save-excursion
  1750.         (back-to-indentation)
  1751.         (delete-region (point) (save-excursion
  1752.                      (beginning-of-line)
  1753.                      (point)))
  1754.         (indent-to-column col))))))
  1755.  
  1756.  
  1757.  
  1758. ;;
  1759. ;; Help-related code.
  1760. ;;
  1761.  
  1762. (defvar tcl-help-saved-dirs nil
  1763.   "Saved help directories.
  1764. If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'
  1765. to update the alist.")
  1766.  
  1767. (defvar tcl-help-alist nil
  1768.   "Alist with command names as keys and filenames as values.")
  1769.  
  1770. (defun tcl-help-snarf-commands (dirlist)
  1771.   "Build alist of commands and filenames."
  1772.   (while dirlist
  1773.     (let ((files (directory-files (car dirlist) t)))
  1774.       (while files
  1775.     (if (and (file-directory-p (car files))
  1776.          (not
  1777.           (let ((fpart (file-name-nondirectory (car files))))
  1778.             (or (equal fpart ".")
  1779.             (equal fpart "..")))))
  1780.         (let ((matches (directory-files (car files) t)))
  1781.           (while matches
  1782.         (or (file-directory-p (car matches))
  1783.             (setq tcl-help-alist
  1784.               (cons
  1785.                (cons (file-name-nondirectory (car matches))
  1786.                  (car matches))
  1787.                tcl-help-alist)))
  1788.         (setq matches (cdr matches)))))
  1789.     (setq files (cdr files))))
  1790.     (setq dirlist (cdr dirlist))))
  1791.  
  1792. (defun tcl-reread-help-files ()
  1793.   "Set up to re-read files, and then do it."
  1794.   (interactive)
  1795.   (message "Building Tcl help file index...")
  1796.   (setq tcl-help-saved-dirs tcl-help-directory-list)
  1797.   (setq tcl-help-alist nil)
  1798.   (tcl-help-snarf-commands tcl-help-directory-list)
  1799.   (message "Building Tcl help file index...done"))
  1800.  
  1801. (defun tcl-current-word (flag)
  1802.   "Return current command word, or nil.
  1803. If FLAG is nil, just uses `current-word'.
  1804. Otherwise scans backward for most likely Tcl command word."
  1805.   (if (and flag
  1806.        (memq major-mode '(tcl-mode inferior-tcl-mode)))
  1807.       (condition-case nil
  1808.       (save-excursion
  1809.         ;; Look backward for first word actually in alist.
  1810.         (if (bobp)
  1811.         ()
  1812.           (while (and (not (bobp))
  1813.               (not (tcl-real-command-p)))
  1814.         (backward-sexp)))
  1815.         (if (assoc (current-word) tcl-help-alist)
  1816.         (current-word)))
  1817.     (error nil))
  1818.     (current-word)))
  1819.  
  1820. ;;;###autoload
  1821. (defun tcl-help-on-word (command &optional arg)
  1822.   "Get help on Tcl command.  Default is word at point.
  1823. Prefix argument means invert sense of `tcl-use-smart-word-finder'."
  1824.   (interactive
  1825.    (list
  1826.     (progn
  1827.       (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
  1828.       (tcl-reread-help-files))
  1829.       (let ((word (tcl-current-word
  1830.            (if current-prefix-arg
  1831.                (not tcl-use-smart-word-finder)
  1832.              tcl-use-smart-word-finder))))
  1833.     (completing-read
  1834.      (if (or (null word) (string= word ""))
  1835.          "Help on Tcl command: "
  1836.        (format "Help on Tcl command (default %s): " word))
  1837.      tcl-help-alist nil t)))
  1838.     current-prefix-arg))
  1839.   (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
  1840.       (tcl-reread-help-files))
  1841.   (if (string= command "")
  1842.       (setq command (tcl-current-word
  1843.              (if arg
  1844.              (not tcl-use-smart-word-finder)
  1845.                tcl-use-smart-word-finder))))
  1846.   (let* ((help (get-buffer-create "*Tcl help*"))
  1847.      (cell (assoc command tcl-help-alist))
  1848.      (file (and cell (cdr cell))))
  1849.     (set-buffer help)
  1850.     (delete-region (point-min) (point-max))
  1851.     (if file
  1852.     (progn
  1853.       (insert "*** " command "\n\n")
  1854.       (insert-file-contents file))
  1855.       (if (string= command "")
  1856.       (insert "Magical Pig!")
  1857.     (insert "Tcl command " command " not in help\n")))
  1858.     (set-buffer-modified-p nil)
  1859.     (goto-char (point-min))
  1860.     (display-buffer help)))
  1861.  
  1862.  
  1863.  
  1864. ;;
  1865. ;; Other interactive stuff.
  1866. ;;
  1867.  
  1868. (defvar tcl-previous-dir/file nil
  1869.   "Record last directory and file used in loading.
  1870. This holds a cons cell of the form `(DIRECTORY . FILE)'
  1871. describing the last `tcl-load-file' command.")
  1872.  
  1873. (defun tcl-load-file (file &optional and-go)
  1874.   "Load a Tcl file into the inferior Tcl process.
  1875. Prefix argument means switch to the Tcl buffer afterwards."
  1876.   (interactive
  1877.    (list
  1878.     ;; car because comint-get-source returns a list holding the
  1879.     ;; filename.
  1880.     (car (comint-get-source "Load Tcl file: "
  1881.                 (or (and
  1882.                  (eq major-mode 'tcl-mode)
  1883.                  (buffer-file-name))
  1884.                 tcl-previous-dir/file)
  1885.                 '(tcl-mode) t))
  1886.     current-prefix-arg))
  1887.   (comint-check-source file)
  1888.   (setq tcl-previous-dir/file (cons (file-name-directory file)
  1889.                     (file-name-nondirectory file)))
  1890.   (tcl-send-string (inferior-tcl-proc)
  1891.            (format inferior-tcl-source-command (tcl-quote file)))
  1892.   (if and-go (switch-to-tcl t)))
  1893.  
  1894. (defun tcl-restart-with-file (file &optional and-go)
  1895.   "Restart inferior Tcl with file.
  1896. If an inferior Tcl process exists, it is killed first.
  1897. Prefix argument means switch to the Tcl buffer afterwards."
  1898.   (interactive
  1899.    (list
  1900.     (car (comint-get-source "Restart with Tcl file: "
  1901.                 (or (and
  1902.                  (eq major-mode 'tcl-mode)
  1903.                  (buffer-file-name))
  1904.                 tcl-previous-dir/file)
  1905.                 '(tcl-mode) t))
  1906.     current-prefix-arg))
  1907.   (let* ((buf (if (eq major-mode 'inferior-tcl-mode)
  1908.           (current-buffer)
  1909.         inferior-tcl-buffer))
  1910.      (proc (and buf (get-process buf))))
  1911.     (cond
  1912.      ((not (and buf (get-buffer buf)))
  1913.       ;; I think this will be ok.
  1914.       (inferior-tcl tcl-application)
  1915.       (tcl-load-file file and-go))
  1916.      ((or
  1917.        (not (comint-check-proc buf))
  1918.        (yes-or-no-p
  1919.     "A Tcl process is running, are you sure you want to reset it? "))
  1920.       (save-excursion
  1921.     (comint-check-source file)
  1922.     (setq tcl-previous-dir/file (cons (file-name-directory file)
  1923.                       (file-name-nondirectory file)))
  1924.     (comint-exec (get-buffer-create buf)
  1925.              (if proc
  1926.              (process-name proc)
  1927.                "inferior-tcl")
  1928.              tcl-application file tcl-command-switches)
  1929.     (if and-go (switch-to-tcl t)))))))
  1930.  
  1931. ;; FIXME I imagine you can do this under Emacs 18.  I just don't know
  1932. ;; how.
  1933. (defun tcl-auto-fill-mode (&optional arg)
  1934.   "Like `auto-fill-mode', but controls filling of Tcl comments."
  1935.   (interactive "P")
  1936.   (and (not tcl-using-emacs-19)
  1937.        (error "You must use Emacs 19 to get this feature."))
  1938.   ;; Following code taken from "auto-fill-mode" (simple.el).
  1939.   (prog1
  1940.       (setq auto-fill-function
  1941.         (if (if (null arg)
  1942.             (not auto-fill-function)
  1943.           (> (prefix-numeric-value arg) 0))
  1944.         'tcl-do-auto-fill
  1945.           nil))
  1946.     ;; Update mode line.  FIXME I'd use force-mode-line-update, but I
  1947.     ;; don't know if it exists in v18.
  1948.     (set-buffer-modified-p (buffer-modified-p))))
  1949.  
  1950. (defun tcl-electric-hash (&optional count)
  1951.   "Insert a `#' and quote if it does not start a real comment.
  1952. Prefix arg is number of `#'s to insert.
  1953. See variable `tcl-electric-hash-style' for description of quoting
  1954. styles."
  1955.   (interactive "p")
  1956.   (or count (setq count 1))
  1957.   (if (> count 0)
  1958.       (let ((type
  1959.          (if (eq tcl-electric-hash-style 'smart)
  1960.          (if (> count 3)    ; FIXME what is "smart"?
  1961.              'quote
  1962.            'backslash)
  1963.            tcl-electric-hash-style))
  1964.         comment)
  1965.     (if type
  1966.         (progn
  1967.           (save-excursion
  1968.         (insert "#")
  1969.         (setq comment (tcl-in-comment)))
  1970.           (delete-char 1)
  1971.           (and tcl-explain-indentation (message "comment: %s" comment))
  1972.           (cond
  1973.            ((eq type 'quote)
  1974.         (if (not comment)
  1975.             (insert "\"")))
  1976.            ((eq type 'backslash)
  1977.         ;; The following will set count to 0, so the
  1978.         ;; insert-char can still be run.
  1979.         (if (not comment)
  1980.             (while (> count 0)
  1981.               (insert "\\#")
  1982.               (setq count (1- count)))))
  1983.            (t nil))))
  1984.     (insert-char ?# count))))
  1985.  
  1986. (defun tcl-hashify-buffer ()
  1987.   "Quote all `#'s in current buffer that aren't Tcl comments."
  1988.   (interactive)
  1989.   (save-excursion
  1990.     (goto-char (point-min))
  1991.     (if (and tcl-pps-has-arg-6 tcl-use-hairy-comment-detector)
  1992.     (let (state
  1993.           result)
  1994.       (while (< (point) (point-max))
  1995.         (setq result (tcl-hairy-scan-for-comment state (point-max) t))
  1996.         (if (car result)
  1997.         (beginning-of-line 2)
  1998.           (backward-char)
  1999.           (if (eq ?# (following-char))
  2000.           (insert "\\"))
  2001.           (forward-char))
  2002.         (setq state (cdr result))))
  2003.       (while (and (< (point) (point-max))
  2004.           (search-forward "#" nil 'move))
  2005.     (if (tcl-real-comment-p)
  2006.         (beginning-of-line 2)
  2007.       ;; There's really no good way for the simple converter to
  2008.       ;; work.  So we just quote # if it isn't already quoted.
  2009.       ;; Bogus, but it works.
  2010.       (backward-char)
  2011.       (if (not (eq ?\\ (preceding-char)))
  2012.           (insert "\\"))
  2013.       (forward-char))))))
  2014.  
  2015. (defun tcl-indent-for-comment ()
  2016.   "Indent this line's comment to comment column, or insert an empty comment.
  2017. Is smart about syntax of Tcl comments.
  2018. Parts of this were taken from indent-for-comment (simple.el)."
  2019.   (interactive "*")
  2020.   (end-of-line)
  2021.   (or (tcl-in-comment)
  2022.       (progn
  2023.     ;; Not in a comment, so we have to insert one.  Create an
  2024.     ;; empty comment (since there isn't one on this line).  If
  2025.     ;; line is not blank, make sure we insert a ";" first.
  2026.     (skip-chars-backward " \t")
  2027.     (let ((eolpoint (point)))
  2028.       (beginning-of-line)
  2029.       (if (/= (point) eolpoint)
  2030.           (progn
  2031.         (goto-char eolpoint)
  2032.         (insert
  2033.          (if (tcl-real-command-p) "" ";")
  2034.          "# ")
  2035.         (backward-char))))))
  2036.   ;; Point is just after the "#" starting a comment.  Move it as
  2037.   ;; appropriate.
  2038.   (let* ((indent (if comment-indent-hook
  2039.              (funcall comment-indent-hook)
  2040.            (funcall comment-indent-function)))
  2041.      (begpos (progn
  2042.            (backward-char)
  2043.            (point))))
  2044.     (if (/= begpos indent)
  2045.     (progn
  2046.       (skip-chars-backward " \t" (save-excursion
  2047.                        (beginning-of-line)
  2048.                        (point)))
  2049.       (delete-region (point) begpos)
  2050.       (indent-to indent)))
  2051.     (looking-at comment-start-skip)    ; Always true.
  2052.     (goto-char (match-end 0))
  2053.     ;; I don't like the effect of the next two.
  2054.     ;;(skip-chars-backward " \t" (match-beginning 0))
  2055.     ;;(skip-chars-backward "^ \t" (match-beginning 0))
  2056.     ))
  2057.  
  2058. ;; The following was inspired by the Tcl editing mode written by
  2059. ;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>.  His version also
  2060. ;; attempts to snarf the command line options from the command line,
  2061. ;; but I didn't think that would really be that helpful (doesn't seem
  2062. ;; like it owould be right enough.  His version also looks for the
  2063. ;; "#!/bin/csh ... exec" hack, but that seemed even less useful.
  2064. ;; FIXME should make sure that the application mentioned actually
  2065. ;; exists.
  2066. (defun tcl-guess-application ()
  2067.   "Attempt to guess Tcl application by looking at first line.
  2068. The first line is assumed to look like \"#!.../program ...\"."
  2069.   (save-excursion
  2070.     (goto-char (point-min))
  2071.     (if (looking-at "#![^ \t]*/\\([^ \t\n/]+\\)\\([ \t]\\|$\\)")
  2072.     (progn
  2073.       (make-local-variable 'tcl-application)
  2074.       (setq tcl-application (buffer-substring (match-beginning 1)
  2075.                           (match-end 1)))))))
  2076.  
  2077. ;; This only exists to put on the menubar.  I couldn't figure out any
  2078. ;; other way to do it.  FIXME should take "number of #-marks"
  2079. ;; argument.
  2080. (defun tcl-uncomment-region (beg end)
  2081.   "Uncomment region."
  2082.   (interactive "r")
  2083.   (comment-region beg end -1))
  2084.  
  2085.  
  2086.  
  2087. ;;
  2088. ;; XEmacs menu support.
  2089. ;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),
  2090. ;; who wrote a different Tcl mode.
  2091. ;; We also have support for menus in FSF.  We do this by
  2092. ;; loading the XEmacs menu emulation code.
  2093. ;;
  2094.  
  2095. (defun tcl-popup-menu (e)
  2096.   (interactive "@e")
  2097.   (and tcl-using-emacs-19
  2098.        (not tcl-using-xemacs-19)
  2099.        (if tcl-using-emacs-19-23
  2100.        (require 'lmenu)
  2101.      ;; CAVEATS:
  2102.      ;; * lmenu.el provides 'menubar, which is bogus.
  2103.      ;; * lmenu.el causes menubars to be turned on everywhere.
  2104.      ;;   Doubly bogus!
  2105.      ;; Both of these problems are fixed in Emacs 19.23.  People
  2106.      ;; using an Emacs before that just suffer.
  2107.      (require 'menubar "lmenu")))  ;; This is annoying
  2108.   ;; IMHO popup-menu should be autoloaded in FSF Emacs.  Oh well.
  2109.   (popup-menu tcl-xemacs-menu))
  2110.  
  2111.  
  2112.  
  2113. ;;
  2114. ;; Quoting and unquoting functions.
  2115. ;;
  2116.  
  2117. ;; This quoting is sufficient to protect eg a filename from any sort
  2118. ;; of expansion or splitting.  Tcl quoting sure sucks.
  2119. (defun tcl-quote (string)
  2120.   "Quote STRING according to Tcl rules."
  2121.   (mapconcat (function (lambda (char)
  2122.              (if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ?  ?\;))
  2123.                  (concat "\\" (char-to-string char))
  2124.                (char-to-string char))))
  2125.          string ""))
  2126.  
  2127.  
  2128.  
  2129. ;;
  2130. ;; Bug reporting.
  2131. ;;
  2132.  
  2133. (and (fboundp 'eval-when-compile)
  2134.      (eval-when-compile
  2135.        (require 'reporter)))
  2136.  
  2137. (defun tcl-submit-bug-report ()
  2138.   "Submit via mail a bug report on Tcl mode."
  2139.   (interactive)
  2140.   (require 'reporter)
  2141.   (and
  2142.    (y-or-n-p "Do you really want to submit a bug report on Tcl mode? ")
  2143.    (reporter-submit-bug-report
  2144.     tcl-maintainer
  2145.     (concat "Tcl mode " tcl-version)
  2146.     '(tcl-indent-level
  2147.       tcl-continued-indent-level
  2148.       tcl-auto-newline
  2149.       tcl-tab-always-indent
  2150.       tcl-use-hairy-comment-detector
  2151.       tcl-electric-hash-style
  2152.       tcl-help-directory-list
  2153.       tcl-use-smart-word-finder
  2154.       tcl-application
  2155.       tcl-command-switches
  2156.       tcl-prompt-regexp
  2157.       inferior-tcl-source-command
  2158.       tcl-using-emacs-19
  2159.       tcl-using-emacs-19-23
  2160.       tcl-using-xemacs-19
  2161.       tcl-proc-list
  2162.       tcl-proc-regexp
  2163.       tcl-typeword-list
  2164.       tcl-keyword-list
  2165.       tcl-font-lock-keywords
  2166.       tcl-pps-has-arg-6))))
  2167.  
  2168.  
  2169.  
  2170. (provide 'tcl)
  2171.  
  2172. ;;; tcl.el ends here
  2173.